# -*- coding:utf-8 -*-

from httppack import sheetScan
from httppack import sheetAccept
from Models import *
import sqlite3
import json 
from transpack import sheetDao, userDao, workDao, mailDao
from emailpack import sender
import time, threading


work1 = None
work2 = None
work3 = None

def scanAndAcceptSheet():
	users = userDao.list()
	for user in users:
		sheets = sheetScan.getSheetList(user.name)
		if len(sheets) == 0:
			print u'用户' + user.name + u',网上没有新工单'
		else:
			print u'用户' + user.name + u'有' + str(len(sheets)) + u'个新工单'
		for sheet in sheets:
			res = sheetAccept.accept(sheet)
			
			if json.loads(res,encoding='gbk')['statusCode'] == '200':
				sheet.processedFlag = True
			else:
				sheet.processedFlat = False
			sheet.reason = json.loads(res,encoding='gbk')['message']
			sheetDao.save(sheet)
			mail = Mail(user.email,u'新工单',u'接收到一个新工单:' + sheet.title)
			mailDao.save(mail)
	

def fromDbCommit():
	sheets = sheetDao.list(processedFlag=0)
	for sheet in sheets:
		res = sheetAccept.accept(sheet)
		if json.loads(res,encoding='gbk')['statusCode'] == '200':
			sheet.processedFlag = True
		else:
			sheet.processedFlat = False
		sheet.reason = json.loads(res,encoding='gbk')['message']
		sheetDao.update(sheet)
		
def fromDbSendMail():
	mails = mailDao.list(sendFlag=0)
	for mail in mails:
		mail.sendFlag = sendMail(mail.receiver,mail.sub,mail.content)
		mailDao.update(mail)
		

def sendMail(*args, **kw):
	return sender.send_mail(*args, **kw)
		
def addUser(name, email):
	user = User(name,email)
	userDao.save(user)

def listUsers():
	return userDao.list()

def updateUser(id, name , email):
	user = User(name , email)
	user.id=id
	userDao.update(user)

def deleteUser(*args,**kw):
	userDao.delete(*args,**kw)
	
def listSheets(*args,**kw):
	return sheetDao.list(*args, **kw)

def getSheet(*args,**kw):
	return sheetDao.get(*args,**kw)
	
def updateSheet(*args,**kw):
	sheetDao.update(*args,**kw)
	
def deleteSheet(*args,**kw):
	sheetDao.delete(*args,**kw)

def getWork(*args,**kw):
	return workDao.get(*args,**kw)

def setWork(*args, **kw):
	workDao.update(*args, **kw)
	
def work1():
	while True:
		print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + u' 扫描网络上的工单开始'
		scanAndAcceptSheet()
		print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + u' 扫描网络上的工单结束'
		time.sleep(workDao.get(1))
		

def startWork1():
	global work1
	work1 = threading.Thread(target=work1, name='LoopThread')
	work1.start()

def updateWork1(value):
	workDao.update(1,value)

	
def work2():
	while True:
		print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + u' 扫描数据库里的工单开始'
		fromDbCommit()
		print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + u' 扫描数据库里的工单结束'
		time.sleep(workDao.get(2))
		
def startWork2():
	global work2
	work2 = threading.Thread(target=work2, name='LoopThread')
	work2.start()
	
def updateWork2(value):
	workDao.update(2,value)
	
def work3():
	while True:
		fromDbSendMail()
		time.sleep(workDao.get(3))

def startWork3():
	global work3
	work3 = threading.Thread(target=work3, name='LoopThread')
	work3.start()

if __name__=='__main__':
	#sendMail(['suzhe1@ha.chinamobile.com'],'hello','helloworld')
	#mailDao.save(Mail('111','111','111'))
	#print mailDao.list(sendFlag=1)
	addUser('ss','ss')